# -*- coding: utf-8 -*-
import scrapy
from Aoisolas.items import AoisolasItem


class AoisolaspiderSpider(scrapy.Spider):
    name = 'Aoisolaspider'
    allowed_domains = ['www.mm131.com']
    meta = {
        'dont_redirect': True,  # 禁止网页重定向
        'handle_httpstatus_list': [301, 302]  # 对哪些异常返回进行处理
    }
    start_urls = ['http://www.mm131.com/xinggan/',
                  'http://www.mm131.com/qingchun/',
                  'http://www.mm131.com/xiaohua/',
                  'http://www.mm131.com/chemo/',
                  'http://www.mm131.com/qipao/',
                  'http://www.mm131.com/mingxing/']

    def parse(self, response):
        list = response.xpath("//dl[@class='list-left public-box']//dd[position()<21]")
        for img in list:
            imgname = img.xpath(".//a/text()").extract_first()
            # 这里并不是取缩略图而是取这个主题的页面链接
            imgurl = img.xpath(".//a/@href").extract_first()
            imgurl2 = str(imgurl)
            print(imgurl2)
            next_url = response.xpath("//dl[@class='list-left public-box']//dd[last()]//a[last()-1]//@href").extract_first()
            if next_url is not None:
                yield response.follow(next_url, callback=self.parse)

            yield scrapy.Request(imgurl2, callback=self.content)

    def content(self, response):
        item = AoisolasItem()
        item['name'] = response.xpath("//div[@class='content']/h5/text()").extract_first()
        item['Imgurl'] = response.xpath("//div[@class='content-pic']//img//@src").extract()
        yield item
        # 提取图片 存入文件夹
        print(item['name'])
        print(item['Imgurl'])
        next_url = response.xpath("//div[@class='content-page']/a[last()]/@href").extract_first()
        if next_url is not None:
            # 下一页
            yield response.follow(next_url, callback=self.content)